﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Xsl;

namespace XMLParse
{
    class Program
    {
        static void Main()
        {

            //XslCompiledTransform xslt = new XslCompiledTransform();
            //xslt.Load("../../html_transform1.xsl");
            //xslt.Transform("../../library_bg_transform1.xml", "../../test.html");


            //task 2
            XmlDocument doc = new XmlDocument();
            doc.Load("..\\..\\catalogue.xml");
            XmlNode rootNode = doc.DocumentElement;

            //task 6
            //XDocument xmlDoc = XDocument.Load("../../catalogue.xml");
            //var songs =
            //    from song in xmlDoc.Descendants("song")
            //    select song.Element("title").Value;

            //foreach (var song in songs)
            //{
            //    Console.WriteLine(song);
            //}

            //task 5
            //using (XmlTextReader reader = new XmlTextReader("../../catalogue.xml"))
            //{
            //    while (reader.Read())
            //    {
            //        if ((reader.NodeType == XmlNodeType.Element) &&
            //            (reader.Name == "song"))
            //        {
            //            while (reader.NodeType == XmlNodeType.Element && reader.Name != "title")
            //            {
            //                reader.Read();
            //            }
            //            Console.WriteLine(reader.ReadElementString());
            //        }
            //    }
            //}

            //task 4
            Console.WriteLine("Root node: {0}", rootNode.Name);
            bool inDelete = true;
            while (inDelete)
            {
                foreach (XmlNode node in rootNode.ChildNodes)
                {
                    string price = node["price"].InnerText;
                    if (int.Parse(price) > 100)
                    {
                        node.ParentNode.RemoveChild(node);
                        inDelete = true;
                    }
                    else
                        inDelete = false;
                }
            }
            doc.Save("..\\..\\catalogue2.xml");


            //task 2 and 3
            //Dictionary<string,int> dict = new Dictionary<string,int>();
            //foreach (XmlNode node in rootNode.ChildNodes)
            //{
            //    foreach (XmlNode n in node.ChildNodes)
            //    {
            //        if (n.Name == "artist")
            //        {
            //            if (dict.ContainsKey(n.InnerText))
            //            {
            //                dict[n.InnerText] = dict[n.InnerText] + 1;
            //            }
            //            else
            //            {
            //                dict.Add(n.InnerText, 1);
            //            }
            //        }
            //    }
            //}

            //foreach (var item in dict)
            //{
            //    Console.WriteLine("{0} - {1} albums", item.Key, item.Value);
            //}
            
            //string xPathQuery = "catalogue/album/artist";
            //XmlNodeList artistList = doc.SelectNodes(xPathQuery);
            //foreach (XmlNode artist in artistList)
            //{
            //      string artistname = artist.InnerText;
            //      Console.WriteLine(artistname);
            //}
        }
    }
}
